package com.bhs.sansong.ui.setting;

import android.content.Intent;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.ArrayMap;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bhs.sansong.R;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.base.BaseBean;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.utils.CountDownTimerUtils2;
import com.bhs.sansong.utils.MyUtils;
import com.bhs.sansong.utils.SharedPreferencesUtils;
import com.bhs.sansong.utils.ToastUtil;
import com.google.gson.Gson;

import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 编辑支付宝提现
 */
public class AccountCreditedEditActivity extends BaseActivity implements NetCallBack, View.OnClickListener, TextWatcher {


    private TextView mTvTitle;
    private TextView mTvTypeBank;
    private EditText mEtName;
    private EditText mEtOpeningBank;
    private EditText mEtCardNumber;
    private TextView mTvGetCode;
    private Button mBtnSubmit;

    @Override
    public int initLayout() {
        return R.layout.activity_account_credited_edit;
    }

    @Override
    public void initView() {

        mTvTitle = findViewById(R.id.tv_title);
        mTvTypeBank = findViewById(R.id.tv_type_bank);
        mEtName = findViewById(R.id.et_name);
        mEtOpeningBank = findViewById(R.id.et_opening_bank);
        mEtCardNumber = findViewById(R.id.et_card_number);
        mTvGetCode = findViewById(R.id.tv_getCode);
        mBtnSubmit = findViewById(R.id.btn_submit);
        mBtnSubmit.setOnClickListener(this);
        mEtCardNumber.addTextChangedListener(this);
        mEtOpeningBank.addTextChangedListener(this);
    }

    @Override
    public void initData() {
        mTvTitle.setText("绑定收款账户");
        mEtName.setText(SharedPreferencesUtils.getString(mContext, "realName"));
        mEtName.setEnabled(false);

        mTvGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(mTvTypeBank.getText().toString())) {
                    ToastUtil.show("请填写手机号");
                    return;
                }
                networkRequest(netApi.getCode("1", mTvTypeBank.getText().toString()), AccountCreditedEditActivity.this);
            }
        });
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        BaseBean bean = new Gson().fromJson(json, BaseBean.class);
        if (requestCode == 0) {
            if (bean.isStatus()) {
                CountDownTimerUtils2 countDownTimerUtils2 = new CountDownTimerUtils2(mTvGetCode, 60 * 1000, 1000);
                countDownTimerUtils2.start();
            } else {
                ToastUtil.show(bean.getMsg());
            }
        } else {
            if (bean.isStatus()) {
                SharedPreferencesUtils.saveString(mActivity, "alipayAccount", mTvTypeBank.getText().toString());
                Intent i = new Intent();
                i.putExtra("phone", mTvTypeBank.getText().toString());
                setResult(2, i);
                finish();
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }

    @Override
    public void onClick(View view) {
        if (!MyUtils.isPhone(mTvTypeBank.getText().toString())) {
            ToastUtil.show("请输入正确的手机号");
            return;
        }
        Map<String, Object> map = new ArrayMap<>();
        map.put("platform", "logistics");
        map.put("alipay", mTvTypeBank.getText().toString());
        map.put("truename", mEtName.getText().toString());
        map.put("verify_code", mEtCardNumber.getText().toString());
        networkRequest(netApi.bind_alipay(map), this, 2);
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        verify();
    }

    private void verify() {
        boolean verify = true;
        if (TextUtils.isEmpty(mTvTypeBank.getText())) {
            verify = false;
        }
        if (TextUtils.isEmpty(mEtCardNumber.getText())) {
            verify = false;
        }
//        if (TextUtils.isEmpty(mEtOpeningBank.getText())) {
//            verify = false;
//        }
        mBtnSubmit.setEnabled(verify);
        mBtnSubmit.setBackgroundColor(verify ? Color.parseColor("#485EF4") : Color.parseColor("#7F485EF4"));
    }
}